#include "pv.h"
   extern int p();
extern int v();
   extern int initsem();
   extern int fork();
   extern int sleep();
   
    int main()
   {
    key_t plan=0x200;
    key_t orange=0x220;
    key_t apple=0x240;
    int plantnum,orangenum,applenum;
    if ((plantnum=initsem(plan,1))<0) exit(1);
  if ((orangenum=initsem(orange,0))<0) exit(1);
   if ((applenum=initsem(apple,0))<0) exit(1);
   printf("plant   进程plant的信号量标识符%d,它的初始值为%d  ",
            plantnum,semctl(plantnum, 0, GETVAL)); 
   printf("orange   进程orange的信号量标识符%d,它的初始值为%d ",
             orangenum,semctl(orangenum, 0, GETVAL)); 
     printf("apple   进程apple的信号量标识符%d,它的初始值为%d\n",                                                                                                             
            applenum,semctl(applenum, 0, GETVAL)); 
          
   int pid1=fork();
   int pid2 = fork();      
if(pid1>0&&pid2>0){


    for(int i =0;i<10; i++){

                   
        p(plantnum);printf("父亲放了一个苹果\n");v(applenum)
		;
	sleep(1);
    }
 }
else if(pid1>0&&pid2==0){

    for(int i =0;i<10 ;i++){


       p(plantnum);printf("母亲放了一个橘子\n");v(orangenum);
       sleep(1);
    }


 }
 else if (pid1==0&&pid2>0){
  
   for(int i =0;i<10 ;i++)
   {

       p(orangenum);printf("儿子吃一个橘子\n");v(plantnum);
   }                                                                                                                                                                 
 }
 else
 {

   for(int i =0;i<10 ;i++){


        p(applenum);
     printf("女儿吃一个苹果\n");
   v(plantnum);
 

    }
 
 }
 return 0;                                                                                                                                                            
 }
